package com.xwj.kafka

import java.util.{Date, Properties}

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

/**
  * Created by thinkjoy on 2017/8/10.
  * 生产者
  */
object KafkaProduce {

  def main(args: Array[String]): Unit = {

    val TOPIC="thinkjoys"
    val props=new Properties()
    props.put("bootstrap.servers","192.168.0.78:9092")
    props.put("zookeeper.connect","192.168.0.78:2181,192.168.0.75:2181,192.168.0.76:2181")
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    props.put("metadata.broker.list", "192.168.0.78:9092,192.168.0.75:9092,192.168.0.76:9092")
    val producter=new KafkaProducer[String,String](props)
    for(i<-1 to 50){
      var record=new ProducerRecord(TOPIC,"key","hello "+i)
      println("要传送到kafka数据:"+record)
      producter.send(record)
    }
    val record=new ProducerRecord(TOPIC,"key","the end"+new Date)
    println("要传送到kafka数据:"+record)
    producter.send(record)
    producter.close()
  }
}
